package server.presentacion;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import server.comunicaciones.IServer;
import server.comunicaciones.KenkenServer;
import server.comunicaciones.Server;
import server.comunicaciones.SudokuServer;
import server.persistencia.ORMConstants;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * @author Administrador
 */
@SuppressWarnings("serial")
public class JFServer extends JFrame {

	private javax.swing.JPanel jContentPane = null;
	private JButton jbConectar = null;
	
	private Server server;

	private JScrollPane jScrollPane = null;
	private JEditorPane jepLog = null;
	private JRadioButton rdbtnSqlServer;
	private JRadioButton rdbtnKenken;
	private JRadioButton rdbtnSistemaDeFicheros;
	private JButton btnCarpetaDeSudokus;
	private JSeparator separator;
	private JSeparator separator_1;
	private JSeparator separator_2;
	private JSeparator separator_3;
	private JLabel lblServidorDelSudoku;
	private JSeparator separator_4;
	private JLabel label;
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJbConectar() {
		if (jbConectar == null) {
			jbConectar = new JButton();
			jbConectar.setBounds(231, 249, 132, 33);
			jbConectar.setText("Conectar");
			jbConectar.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					conectarODesconectar();
				}
			});
		}
		return jbConectar;
	}
     /**
     * 
     */
    protected void conectarODesconectar() {
        if (this.jbConectar.getText().equals("Conectar")) {
            try {
            	if (rdbtnSqlServer.isSelected() || rdbtnSistemaDeFicheros.isSelected()){
            		this.server = new SudokuServer(this);
            		int tipoPersistencia=(this.rdbtnSistemaDeFicheros.isSelected() ? ORMConstants.FILE_SYSTEM : ORMConstants.SQLSERVER);          	
                	this.server.conectar(tipoPersistencia, this.btnCarpetaDeSudokus.getText());
                	this.rdbtnSistemaDeFicheros.setEnabled(false);
                	this.rdbtnSqlServer.setEnabled(false);
                	this.rdbtnKenken.setEnabled(false);
    	            log("Conectado");
    	            this.jbConectar.setText("Desconectar");
            	}else if(rdbtnKenken.isSelected()){
            		this.server = new KenkenServer(this);
            		int tipoPersistencia=(this.rdbtnSistemaDeFicheros.isSelected() ? ORMConstants.FILE_SYSTEM : ORMConstants.SQLSERVER);          	
                	this.server.conectar(tipoPersistencia, this.btnCarpetaDeSudokus.getText());
                	this.rdbtnSistemaDeFicheros.setEnabled(false);
                	this.rdbtnSqlServer.setEnabled(false);
                	this.rdbtnKenken.setEnabled(false);
    	            log("Conectado");
    	            this.jbConectar.setText("Desconectar");
            	}   
            }
            catch (Exception ex) {
                logError(ex.toString());
            }
        } else {
            try {          	
	            this.server.desconectar();
            	this.rdbtnSistemaDeFicheros.setEnabled(true);
            	this.rdbtnSqlServer.setEnabled(true);
            	this.rdbtnKenken.setEnabled(true);
	            log("Desconectado");
	            this.jbConectar.setText("Conectar");
            }
            catch (Exception ex) {
                logError("Desconectar");
            }
        }
    }
	/**
     * @param string
     */
    public synchronized void logError(String mensaje) {
        mensaje="<font color=red>" + mensaje + "</font>" + this.jepLog.getText();
        jepLog.setText(mensaje);
        jepLog.select(0, 0);
    }
    /**
     * @param string
     */
    private void log(String mensaje) {
        mensaje="<font color=blue>" + mensaje + "</font>" + this.jepLog.getText();
        jepLog.setText(mensaje);
        jepLog.select(0, 0);
    }
    /**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */    
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(112, 293, 406, 153);
			jScrollPane.setViewportView(getJepLog());
		}
		return jScrollPane;
	}
	/**
	 * This method initializes jEditorPane	
	 * 	
	 * @return javax.swing.JEditorPane	
	 */    
	private JEditorPane getJepLog() {
		if (jepLog == null) {
			jepLog = new JEditorPane();
			jepLog.setContentType("text/html");
		}
		return jepLog;
	}
	
	// *********** MAIN ***********
    public static void main(String[] args) {
        JFServer j=new JFServer();
        j.setVisible(true);
    }
	/**
	 * This is the default constructor
	 */
	public JFServer() {
		super();
		initialize();
		ButtonGroup grupo=new ButtonGroup();
		grupo.add(this.rdbtnSqlServer);
		grupo.add(this.rdbtnSistemaDeFicheros);
		grupo.add(this.rdbtnKenken);
		
        // this.sudokuServer=new sudokuServer(this);

	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(641,495);
		this.setContentPane(getJContentPane());
		this.setTitle("Servidor");
		this.addWindowListener(new java.awt.event.WindowAdapter() { 
			public void windowClosing(java.awt.event.WindowEvent e) {    
				System.exit(0);
			}
		});
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJbConectar(), null);
			jContentPane.add(getJScrollPane(), null);
			
			rdbtnSqlServer = new JRadioButton("BBDD sudoku");
			rdbtnSqlServer.setBounds(354, 95, 141, 23);
			jContentPane.add(rdbtnSqlServer);
			
			rdbtnSistemaDeFicheros = new JRadioButton("Sistema de ficheros");
			rdbtnSistemaDeFicheros.setSelected(true);
			rdbtnSistemaDeFicheros.setBounds(352, 128, 155, 23);
			jContentPane.add(rdbtnSistemaDeFicheros);
			
			rdbtnKenken = new JRadioButton("BBDD kenken");
			rdbtnKenken.setSelected(true);
			rdbtnKenken.setBounds(112, 128, 150, 23);
			jContentPane.add(rdbtnKenken);
			
			btnCarpetaDeSudokus = new JButton("/Users/. . . /. . . ");
			btnCarpetaDeSudokus.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					ubicacionSudokus();
				}
			});
			btnCarpetaDeSudokus.setBounds(324, 180, 170, 23);
			jContentPane.add(btnCarpetaDeSudokus);
			
			separator = new JSeparator();
			separator.setOrientation(SwingConstants.VERTICAL);
			separator.setBounds(300, 58, 7, 174);
			jContentPane.add(separator);
			
			separator_1 = new JSeparator();
			separator_1.setOrientation(SwingConstants.VERTICAL);
			separator_1.setBounds(83, 58, 7, 168);
			jContentPane.add(separator_1);
			
			separator_2 = new JSeparator();
			separator_2.setOrientation(SwingConstants.VERTICAL);
			separator_2.setBounds(527, 58, 7, 174);
			jContentPane.add(separator_2);
			
			separator_3 = new JSeparator();
			separator_3.setBounds(83, 229, 445, 9);
			jContentPane.add(separator_3);
			
			lblServidorDelSudoku = new JLabel("Servidor del Kenken");
			lblServidorDelSudoku.setFont(new Font("Tahoma", Font.BOLD, 17));
			lblServidorDelSudoku.setBounds(100, 10, 183, 50);
			jContentPane.add(lblServidorDelSudoku);
			
			separator_4 = new JSeparator();
			separator_4.setBounds(83, 58, 445, 9);
			jContentPane.add(separator_4);
			
			label = new JLabel("Servidor del sudoku");
			label.setFont(new Font("Tahoma", Font.BOLD, 17));
			label.setBounds(324, 10, 183, 50);
			jContentPane.add(label);
		}
		return jContentPane;
	}

    protected void ubicacionSudokus() {
		javax.swing.JFileChooser fc=new JFileChooser();
		int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            this.btnCarpetaDeSudokus.setText(file.getAbsolutePath());
        } 
	}

	/**
     *  
     * @uml.property name="sudokuServer"
     * @uml.associationEnd inverse="jFServer:sudoku.server.comunicaciones.SudokuServer" multiplicity="(0 1)"
     * 
     */
   
    /**
     *  
     * @uml.property name="sudokuServer"
     * 
     */
    /*public Server getSudokuServer() {
        return sudokuServer;
    }

    /**
     *  
     * @uml.property name="sudokuServer"
     * 
     */
    /*public void setSudokuServer(Server sudokuServer) {
        this.sudokuServer = sudokuServer;
    }*/
    
}
